package com.zjx.shop.product.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjx.shop.common.utils.PageParams;
import com.zjx.shop.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjx.shop.common.utils.PageUtils;
import com.zjx.shop.product.dao.BrandDao;
import com.zjx.shop.product.entity.BrandEntity;
import com.zjx.shop.product.service.BrandService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateByIdDetail(BrandEntity brand) {
        //保证冗余字段的数据一致
        this.updateById(brand);
        if (!StringUtils.isEmpty(brand.getName())){
            //如果修改了名字，则品牌分类关系表之中的名字也要修改
            categoryBrandRelationService.updateBrand(brand.getBrandId(), brand.getName());
            //TODO 更新其他关联
        }
    }


    @Override
    public PageUtils queryPage(PageParams params) {
        IPage<BrandEntity> page = this.page(
                new Page<>(params.getPage(),params.getSize()),
                new QueryWrapper<BrandEntity>()
        );

        return new PageUtils(page.getCurrent(),page.getSize(), (long) page.getRecords().size(),page.getRecords());
    }


}